package Datos;
// Generated 15-dic-2012 9:31:07 by Hibernate Tools 3.2.1.GA

import java.util.HashSet;
import java.util.Set;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Set;
import java.util.Vector;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.logging.Level;
import java.util.logging.Logger;



/**
 * Telefono generated by hbm2java
 */
public class Telefono  implements java.io.Serializable {

    private Session session;
     private Integer codigo;
     private String numero;
     private int ciEmpleado;

    public Telefono() {
    }

    public Telefono(String numero, int ciEmpleado) {
       this.numero = numero;
       this.ciEmpleado = ciEmpleado;
    }
   
    public Integer getCodigo() {
        return this.codigo;
    }
    
    public void setCodigo(Integer codigo) {
        this.codigo = codigo;
    }
    public String getNumero() {
        return this.numero;
    }
    
    public void setNumero(String numero) {
        this.numero = numero;
    }
    public int getCiEmpleado() {
        return this.ciEmpleado;
    }
    
    public void setCiEmpleado(int ciEmpleado) {
        this.ciEmpleado = ciEmpleado;
    }


//-------------------------------------------------------------------------
    public int cantTuplas() {
        return HibernateUtil.CantTuplas(resultado());
    }
 //--------------------------------------------------------------------------//
    public void insertar() {

        try {
            int cant = cantTuplas() + 1;
            Session Sesion = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = Sesion.beginTransaction();
            this.setCodigo(cant);
            Sesion.save(this);
            tx.commit();
            Sesion.close();
            JOptionPane.showMessageDialog(null, "SE INSERTARON LOS DATOS CORRECTAMENTE EN LA BD.", "MENSAJE", JOptionPane.WARNING_MESSAGE, null);

        } catch (HibernateException e) {
            System.out.println(" Error al INSERTAR  " + e.getMessage());
            Logger.getLogger(Telefono.class.getName()).log(Level.SEVERE, null, e);
        }

    }
//--------------------------------------------------------------------------//

    public void modificar() {

        try {

            Session Sesion = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = Sesion.beginTransaction();
            Sesion.update(this);
            tx.commit();
            Sesion.close();
            JOptionPane.showMessageDialog(null, "SE MODIFICARON LOS DATOS CORRECTAMENTE EN LA BD.", "MENSAJE", JOptionPane.WARNING_MESSAGE, null);

        } catch (HibernateException e) {
            System.out.println("Error al MODIFICAR  " + e.getMessage());
            Logger.getLogger(Telefono.class.getName()).log(Level.SEVERE, null, e);
        }
    }
//-----------------------------------------------------------------------------//

    public void eliminar() {

        try {

            Session Sesion = HibernateUtil.getSessionFactory().openSession();
            Transaction tx = Sesion.beginTransaction();
            Telefono t = getTelefono();
            Sesion.delete(t);
            tx.commit();
            Sesion.close();
            JOptionPane.showMessageDialog(null, "SE ELIMINARON LOS DATOS CORRECTAMENTE EN LA BD.", "MENSAJE", JOptionPane.WARNING_MESSAGE, null);

        } catch (HibernateException e) {
            System.out.println("Error al ELIMINAR  " + e.getMessage());
            Logger.getLogger(Telefono.class.getName()).log(Level.SEVERE, null, e);
        }
    }
//-----------------------------------------------------------------------------//

    public boolean existeEmpleado(int ci) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction ts = session.getTransaction();
        ts.begin();
        ResultSet rs = null;
        try {
            String query = "SELECT * FROM Telefono "
                    + "WHERE "
                    + "ciEmpleado = " + ci + "";
            PreparedStatement ps = (PreparedStatement) session.connection().prepareStatement(query);
            rs = ps.executeQuery();

        } catch (Exception e) {
            Logger.getLogger(Telefono.class.getName()).log(Level.SEVERE, null, e);
        }

        ts.commit();
        session.close();
        return HibernateUtil.existe(rs);
    }

//--------------------------------------------------------------------------//

    public boolean existeID(int id) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction ts = session.getTransaction();
        ts.begin();
        ResultSet rs = null;
        try {
            String query = "SELECT * FROM Telefono "
                    + "WHERE "
                    + "codigo = " + id + "";
            PreparedStatement ps = (PreparedStatement) session.connection().prepareStatement(query);
            rs = ps.executeQuery();

        } catch (Exception e) {
            Logger.getLogger(Telefono.class.getName()).log(Level.SEVERE, null, e);
        }

        ts.commit();
        session.close();
        return HibernateUtil.existe(rs);
    }

//--------------------------------------------------------------------------//

    public ResultSet EjecutarConsultaSQL(String sql) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction ts = session.getTransaction();
        ts.begin();
        ResultSet rs = null;

        try {
            String query = sql;
            PreparedStatement ps = (PreparedStatement) session.connection().prepareStatement(query);
            rs = ps.executeQuery();
        } catch (Exception e) {
            Logger.getLogger(Telefono.class.getName()).log(Level.SEVERE, null, e);
        }

        ts.commit();
        session.close();
        return rs;
    }

//-----------------------------------------------------------------------------//

    public String listar() {
        ResultSet rs = EjecutarConsultaSQL("select *from Telefono order by codigo;");

        return HibernateUtil.mostrarTuplas(rs, "Lista de Telefono", new String[]{"CODIGO", "NUMERO", "EMPLEADO_CI"},
                new String[]{"codigo", "numero","ciEmpleado"});
    }

//-----------------------------------------------------------------------------//

    public ResultSet resultado() {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction ts = session.getTransaction();
        ts.begin();
        ResultSet rs = null;

        try {
            String query = "SELECT * FROM Telefono order by codigo;";
            PreparedStatement ps = (PreparedStatement) session.connection().prepareStatement(query);
            rs = ps.executeQuery();

        } catch (Exception e) {
            Logger.getLogger(Telefono.class.getName()).log(Level.SEVERE, null, e);
        }

        ts.commit();
        session.close();
        return rs;
    }
//-----------------------------------------------------------------------------//

    public Telefono getTelefono() {

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.getTransaction();
        try {

            tx.begin();
            Telefono t = (Telefono) session.load(Usuario.class, getCodigo());
            return t;
        } catch (Exception e) {
            tx.rollback();
            Logger.getLogger(Telefono.class.getName()).log(Level.SEVERE, null, e);
        }
        return null;
    }

//--------------------------------------------------------------------------//

}


